package com.bbva.kyfb.kyfbjpatlbh.dominiotlbh;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

/**
 * The Class TtlsbcloDataOnDemand.
 */
@Component
@Configurable
public class TtlsbcloDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<Ttlsbclo> data;

	public Ttlsbclo getNewTransientTtlsbclo(int index) {
        Ttlsbclo obj = new Ttlsbclo();
        setAudFmodific(obj, index);
        setAudUsuario(obj, index);
        setCodExtfiche(obj, index);
        setCodPaisfict(obj, index);
        setCodSercrubd(obj, index);
        setCodServimod(obj, index);
        setCodTipmsje(obj, index);
        setDesClasreme(obj, index);
        setHmsCorte(obj, index);
        setXtiCorte(obj, index);
        setXtiEnvrecep(obj, index);
        setXtiModoorde(obj, index);
        setXtiRechazos(obj, index);
        setXtiSubtipor(obj, index);
        setXtiTipoorde(obj, index);
        setXtiValidact(obj, index);
        if (String.valueOf(index).length() > 2){
        	obj.setCodClaseord(String.valueOf(index).substring(0, 2));
        }else {
        	obj.setCodClaseord(String.valueOf(index));
        }
        return obj;
    }

	public void setAudFmodific(Ttlsbclo obj, int index) {
        Date audFmodific = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setAudFmodific(audFmodific);
    }

	public void setAudUsuario(Ttlsbclo obj, int index) {
        String audUsuario = "audUsu_" + index;
        if (audUsuario.length() > 8) {
            audUsuario = audUsuario.substring(0, 8);
        }
        obj.setAudUsuario(audUsuario);
    }

	public void setCodExtfiche(Ttlsbclo obj, int index) {
        String codExtfiche = "c_" + index;
        if (codExtfiche.length() > 3) {
            codExtfiche = codExtfiche.substring(0, 3);
        }
        obj.setCodExtfiche(codExtfiche);
    }

	public void setCodPaisfict(Ttlsbclo obj, int index) {
        Short codPaisfict = new Integer(index).shortValue();
        obj.setCodPaisfict(codPaisfict);
    }

	public void setCodSercrubd(Ttlsbclo obj, int index) {
        Short codSercrubd = new Integer(index).shortValue();
        obj.setCodSercrubd(codSercrubd);
    }

	public void setCodServimod(Ttlsbclo obj, int index) {
        String codServimod = "cod_" + index;
        if (codServimod.length() > 5) {
            codServimod = codServimod.substring(0, 5);
        }
        obj.setCodServimod(codServimod);
    }

	public void setCodTipmsje(Ttlsbclo obj, int index) {
        String codTipmsje = "co_" + index;
        if (codTipmsje.length() > 4) {
            codTipmsje = codTipmsje.substring(0, 4);
        }
        obj.setCodTipmsje(codTipmsje);
    }

	public void setDesClasreme(Ttlsbclo obj, int index) {
        String desClasreme = "desClasreme_" + index;
        if (desClasreme.length() > 20) {
            desClasreme = desClasreme.substring(0, 20);
        }
        obj.setDesClasreme(desClasreme);
    }

	public void setHmsCorte(Ttlsbclo obj, int index) {
        Date hmsCorte = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setHmsCorte(hmsCorte);
    }

	public void setXtiCorte(Ttlsbclo obj, int index) {
        Character xtiCorte = new Character('N');
        obj.setXtiCorte(xtiCorte);
    }

	public void setXtiEnvrecep(Ttlsbclo obj, int index) {
        Character xtiEnvrecep = new Character('N');
        obj.setXtiEnvrecep(xtiEnvrecep);
    }

	public void setXtiModoorde(Ttlsbclo obj, int index) {
        Character xtiModoorde = new Character('N');
        obj.setXtiModoorde(xtiModoorde);
    }

	public void setXtiRechazos(Ttlsbclo obj, int index) {
        Character xtiRechazos = new Character('N');
        obj.setXtiRechazos(xtiRechazos);
    }

	public void setXtiSubtipor(Ttlsbclo obj, int index) {
        Character xtiSubtipor = new Character('N');
        obj.setXtiSubtipor(xtiSubtipor);
    }

	public void setXtiTipoorde(Ttlsbclo obj, int index) {
        Character xtiTipoorde = new Character('N');
        obj.setXtiTipoorde(xtiTipoorde);
    }

	public void setXtiValidact(Ttlsbclo obj, int index) {
        Character xtiValidact = new Character('N');
        obj.setXtiValidact(xtiValidact);
    }

	public Ttlsbclo getSpecificTtlsbclo(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Ttlsbclo obj = data.get(index);
        return Ttlsbclo.findTtlsbclo(obj.getCodClaseord());
    }

	public Ttlsbclo getRandomTtlsbclo() {
        init();
        Ttlsbclo obj = data.get(rnd.nextInt(data.size()));
        return Ttlsbclo.findTtlsbclo(obj.getCodClaseord());
    }

	public boolean modifyTtlsbclo(Ttlsbclo obj) {
        return false;
    }

	public void init() {
        data = Ttlsbclo.findTtlsbcloEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Ttlsbclo' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlbh.dominiotlbh.Ttlsbclo>();
        for (int i = 0; i < 10; i++) {
            Ttlsbclo obj = getNewTransientTtlsbclo(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
